package com.db.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.db.dto.CustMap;
import com.db.service.CustMapService;
import com.db.system.config.DynamicDataSourceContextHolder;
import com.db.system.enums.DataSourceType;
import com.db.system.interfaces.DataSource;

/**
 * 应用层实现
 */
@RestController
@RequestMapping(value = "/")
public class UserController {
 
    @Autowired
    private CustMapService custMapService;
 
    /**
     * 测试配置文件指定
     */
    @RequestMapping(value = "getCustMapByID")
    public CustMap getCustMapByID(String id) throws Exception {
    	CustMap custMap = new CustMap();
    	if (StringUtils.isEmpty(id)) {
    		custMap.setName("id为空啊啊");
    		return custMap;
    	}
    	custMap = custMapService.getCustMapByID(Integer.parseInt(id));
    	System.out.println("getCustMapByID:" + custMap);
        return custMap;
    }
 
    /**
     * 测试默认数据源
     */
    @DataSource
    @RequestMapping(value = "getCustMapByIDA")
    public CustMap getCustMapByIDA(String id) throws Exception {
    	CustMap custMap = new CustMap();
    	if (StringUtils.isEmpty(id)) {
    		custMap.setName("id为空啊啊");
    		return custMap;
    	}
    	custMap = custMapService.getCustMapByID(Integer.parseInt(id));
    	System.out.println("getCustMapByIDA:" + custMap);
        return custMap;
    }
 
    /**
     * 测试注解指定数据源
     */
    @DataSource(value = DataSourceType.MASTER)
    @RequestMapping(value = "getCustMapByIDB")
    public CustMap getCustMapByIDB(String id) throws Exception {
    	CustMap custMap = new CustMap();
    	if (StringUtils.isEmpty(id)) {
    		custMap.setName("id为空啊啊");
    		return custMap;
    	}
    	custMap = custMapService.getCustMapByID(Integer.parseInt(id));
    	System.out.println("getCustMapByIDB:" + custMap);
        return custMap;
    }
 
    /**
     * 测试方法内指定数据源
     */
    @RequestMapping(value = "getCustMapByIDC")
    public CustMap getCustMapByIDC(String id) throws Exception {
    	CustMap custMap = new CustMap();
    	if (StringUtils.isEmpty(id)) {
    		custMap.setName("id为空啊啊");
    		return custMap;
    	}
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE);
        custMap = custMapService.getCustMapByID(Integer.parseInt(id));
        System.out.println("getCustMapByIDC:" + DataSourceType.SLAVE);
        System.out.println("getCustMapByIDC:" + custMap);
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER);
        custMap = custMapService.getCustMapByID(Integer.parseInt(id));
        System.out.println("getCustMapByIDC:" + DataSourceType.MASTER);
        System.out.println("getCustMapByIDC:" + custMap);    
        return custMap;
    }
    
}

